#include "GapStateIdle.h"
#include "GapStateMap.h"
#include "common.pb.h"
#include "GAP.pb.h"
#include "MgwGapProxy.h"
#include <iostream>

namespace tsc
{
namespace sgw
{

using namespace GatewayApplicationPart;
using namespace ProtocolsCommon;

/*******************************************************************************
 *
 * Constructor
 *
 ******************************************************************************/
GapStateIdle::GapStateIdle() :
	GapStateBase("Idle")
{

} // GapStateIdle::GapStateIdle

/*******************************************************************************
 *
 * Destructor
 *
 ******************************************************************************/
GapStateIdle::~GapStateIdle()
{

} // GapStateIdle::~GapStateIdle

//===================================================================
GapStateIdle* GapStateIdle::instance()
{
	static GapStateIdle mInstance;
	return &mInstance;
}

//===================================================================
void
GapStateIdle::handleEventGapConnectionInd(
    GapGenerator*          gapGen,
    GapEventConnectionInd* event,
    EventData*& eventData)
{
    // TODO logging

    gapGen->setMgwIpAddress(event->getMgwIpAddress());

    if(MgwGapProxy::instance().sendGapConnectionCfm(*gapGen))
    {
        gapGen->startT823();

        MgwGapProxy::instance().addConnIdMapping(
                gapGen->getMgwId(),
                event->getSocketConnId());

        gapGen->changeState(GapStateMap::mpStateConnected);
    }
    else
    {
        // TODO logging
        std::cout << "Failed to send GapConnectCfm, gapGen ID=0x" <<
                std::hex << gapGen->getGeneratorId() << std::endl;
    }

    EventData::deleteEvent(eventData);
}
} // namespace sgw
} // namespace tsc
